package com.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.health.pojo.Member;
import com.health.pojo.Order;
import com.health.service.MemberService;
import com.health.vo.PageResult;
import com.health.vo.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference
    private MemberService memberService;

    /**
     * 获取会员id，若id为空，则添加会员
     */
    @PostMapping("/findMemberId")
    public Result findMemberId(@RequestBody Member member) {
        int memberId = memberService.findMemberId(member);

        return new Result(true, "获取会员Id成功", memberId);
    }

    /**
     * 修改会员信息
     */
    @PostMapping("/update")
    public Result update(@RequestBody Member member){
        int count = memberService.update(member);
        if (count > 0) {
            return new Result(true, "修改会员信息成功");
        }
        return new Result(false,"修改会员信息失败");
    }

    /**
     * 新增会员
     */
    @PostMapping("/add")
    public Result add(@RequestBody Member member){
        int count = memberService.add(member);
        if (count > 0) {
            return new Result(true, "添加会员信息成功");
        }
        return new Result(false,"添加会员信息失败");
    }

    /**
     * 删除会员-有'ROLE_ADMIN'权限的才能删除会员
     */
    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("hasRole('ROLE_ADMIN')") //拥有ROLE_ADMIN角色才能访问
    public Result delete(@PathVariable Integer id){
        int count = memberService.deleteById(id);
        if (count > 0) {
            return new Result(true, "删除成功");
        }
        return new Result(false, "删除失败");
    }

    /**
     * 会员列表分页查询
     */
    @GetMapping("/findPage/{pageNow}/{pageSize}")
    public Result findPage(@PathVariable Integer pageNow, @PathVariable Integer pageSize,
                           String query) {
        PageResult<Member> memberList = memberService.findPage(pageNow, pageSize, query);
        return new Result(true, "查询会员列表成功", memberList);
    }

    /**
     * 根据id查询会员
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id){
        Member member = memberService.findById(id);
        return new Result(true,"查询成功",member);
    }
}
